﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Com.Toook.AutoDownloadHtml.DownloadHtml.ParseObj
{
    /// <summary>
    /// 分类页面的一条解析规则。一个页面可以有多个解析规则
    /// </summary>
    public class Rule
    {       
        /// <summary>
        /// 当前步骤
        /// </summary>
        public Step CurrentStep
        {
            get
            {
                return currentStep;
            }            
        }
        
        /// <summary>
        /// 获取最终链接步骤
        /// </summary>
        public Step CurrentLeaf
        {
            get
            {
                return currentLeaf;
            }            
        }
        /// <summary>
        /// 增加步骤
        /// </summary>
        /// <param name="step"></param>
        public void AddStep(Step step)
        {
            stepList.Add(step);
        }
        /// <summary>
        /// 增加详细页面 a href 解析步骤
        /// </summary>
        /// <param name="step"></param>
        public void AddLeaf(Step step)
        {
            leafList.Add(step);
        }
        /// <summary>
        /// 移到下一步.开始位置在所有步骤之前，就像DataReader.Read()
        /// </summary>
        /// <returns></returns>
        public bool NextStep()
        {
            index++;
            if(index < stepList.Count)
            {
                currentStep = stepList[index];
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 移到下一步.开始位置在所有Leaf之前，就像DataReader.Read()
        /// </summary>
        /// <returns></returns>
        public bool NextLeaf()
        {
            indexLeaf++;
            if (indexLeaf < leafList.Count)
            {
                currentLeaf = leafList[indexLeaf];
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 重置Step的位置
        /// </summary>
        public void ResetNextStep()
        {
            index = -1;
        }
        /// <summary>
        /// 重置Leaf的位置
        /// </summary>
        public void ResetNextLeaf()
        {
            indexLeaf = -1;
        }
        public Rule()
        {
            stepList = new List<Step>();
            leafList = new List<Step>();
        }
        /// <summary>
        /// 设置初始大小(包含多少Step步骤)
        /// </summary>
        /// <param name="stepCount"></param>
        //public Rule(int stepCount)
        //{
        //    stepList = new List<Step>(stepCount);
        //}

        private int index = -1;
        private int indexLeaf = -1;
        private Step currentStep = null;
        private Step currentLeaf = null;
        private List<Step> stepList = null;
        private List<Step> leafList = null;
        private Leaf leaf = null;
        /// <summary>
        /// 链接、图片等页节点的解析规则
        /// </summary>
        public Leaf MultiLeaf
        {
            get
            {
                return leaf;
            }
            set
            {
                leaf = value;
            }
        }
    }
}
